/***************************************************************************
 创建者：华磊
 开始时间：         : 2020.12.12
 copyright            : (C) 深圳市华友高科有限公司
 修改说明：（每次有修改就添加一条，带有 修改人，修改时间，修改描述）

 ***************************************************************************
 *   标签指令属性编辑视窗
*                                                                         *
 ***************************************************************************/
#include "labeljmpdelegate.h"
#include <QDebug>
#include "../CommunicateDelegate/communicatenormaldelegate.h"
#include <QMessageBox>
#include "globaldata.h"

LabelJmpDelegate::LabelJmpDelegate(GlobalData *globalDataIn,
                                   CommunicateNormalDelegate *communicateNormalDelegateIn,
                                   LabelJmpView *labelJmpViewIn,
                                   ProgromListView *progromListViewIn,
                                   QWidget *parent):
    QObject(parent)
{
    globalData=globalDataIn;
    communicateNormalDelegate=communicateNormalDelegateIn;
    labelJmpView=labelJmpViewIn;
    progromListView = progromListViewIn;

    connect(progromListView,SIGNAL(getSelectFileName(QString)),this,SLOT(showSelectFileName(QString)));


}

void LabelJmpDelegate::showSelectFileName(QString fileName)
{
    labelJmpView->setProgromFileName(fileName);

}

void LabelJmpDelegate::initialView()
{
    labelJmpView->instruction = "LBL";
    labelJmpView->labelStr = 1;
    labelJmpView->lblDataType = 0;
//    labelJmpView->callLocation = "./";
    labelJmpView->callProgram = "";
    labelJmpView->note = "";
    labelJmpView->selectInstructionFlag = EM_LBLJMP_INSTRUCTION_LBL;

    //labelJmpView->updateView();
}

void LabelJmpDelegate::selectLabelInstructionSlot()
{
    labelJmpView->selectInstructionFlag = EM_LBLJMP_INSTRUCTION_LBL;
    labelJmpView->updateView();

    qDebug("select LBL");
}

void LabelJmpDelegate::selectJmpInstructionSlot()
{
    labelJmpView->selectInstructionFlag = EM_LBLJMP_INSTRUCTION_JMP;
    labelJmpView->updateView();

    qDebug("select JMP");
}

void LabelJmpDelegate::selectEndInstructionSlot()
{
    labelJmpView->selectInstructionFlag = EM_LBLJMP_INSTRUCTION_END;
    labelJmpView->updateView();

    qDebug("select END");
}

void LabelJmpDelegate::selectPauseInstructionSlot()
{
    labelJmpView->selectInstructionFlag = EM_LBLJMP_INSTRUCTION_PAUSE;
    labelJmpView->updateView();

    qDebug("select PAUSE");
}

void LabelJmpDelegate::selectAbortInstructionSlot()
{
    labelJmpView->selectInstructionFlag = EM_LBLJMP_INSTRUCTION_ABORT;
    labelJmpView->updateView();

    qDebug("select ABORT");
}

void LabelJmpDelegate::selectCallInstructionSlot()
{
    labelJmpView->selectInstructionFlag = EM_LBLJMP_INSTRUCTION_CALL;
    labelJmpView->updateView();

    qDebug("select CALL");
}

void LabelJmpDelegate::selectRunInstructionSlot()
{
    labelJmpView->selectInstructionFlag = EM_LBLJMP_INSTRUCTION_RUN;
    labelJmpView->updateView();

    qDebug("select RUN");
}

void LabelJmpDelegate::browseCallProgramSlot(QString oldProgramNameIn)
{
    //todo hualei
    progromListView->show();
    qDebug() << "调用程序";
    int returnFlag;
    std::vector <std::string> fileList;
    communicateNormalDelegate->getRobotProgramFileList(globalData->currentRobotID, fileList, returnFlag);
    if(returnFlag == 0)
    {
        // 获取文件成功
        qDebug()<< "文件列表个数：" << fileList.size();
        QStringList fileList1;
        for(int i = 0; i < (int)fileList.size(); i++)
        {
           fileList1.append(QString::fromStdString(fileList.at(i)));
        }

        progromListView->setProgramFileList(fileList1);
        progromListView->setSelectFile(oldProgramNameIn);
    }
    // 通信失败
    else
    {
         QMessageBox::information(NULL, "Information", "Gsoap获取远程程序文件列表失败！", QMessageBox::Yes );
    }

//                    labelJmpView->callProgram = nameList[nameList.size()-1];
//                    labelJmpView->updateView();


}

void LabelJmpDelegate::clearNoteSlot()
{
    labelJmpView->note.clear();

    qDebug("clear note");
}

void LabelJmpDelegate::finishEditInstructionSlot()
{

    if(EM_LBLJMP_INSTRUCTION_LBL == labelJmpView->selectInstructionFlag)
    {
        labelJmpView->instruction = "LBL";
        if(0 == labelJmpView->lblDataType)
        {
            labelJmpView->instruction += "[" + labelJmpView->labelStr + "]";
        }
        else
        {
            labelJmpView->instruction = labelJmpView->instruction + "[R[" + labelJmpView->labelStr + "]]";
        }
    }
    else if(EM_LBLJMP_INSTRUCTION_JMP == labelJmpView->selectInstructionFlag)
    {
        labelJmpView->instruction = "JMP LBL";
        if(0 == labelJmpView->lblDataType)
        {
            labelJmpView->instruction += "[" + labelJmpView->labelStr + "]";
        }
        else
        {
            labelJmpView->instruction = labelJmpView->instruction + "[R[" + labelJmpView->labelStr + "]]";
        }
    }
    else if(EM_LBLJMP_INSTRUCTION_END == labelJmpView->selectInstructionFlag)
    {
        labelJmpView->instruction = "END";
    }
    else if(EM_LBLJMP_INSTRUCTION_PAUSE == labelJmpView->selectInstructionFlag)
    {
        labelJmpView->instruction = "PAUSE";
    }
    else if(EM_LBLJMP_INSTRUCTION_ABORT == labelJmpView->selectInstructionFlag)
    {
        labelJmpView->instruction = "ABORT";
    }
    else if(EM_LBLJMP_INSTRUCTION_CALL == labelJmpView->selectInstructionFlag)
    {
        QString str = "CALL ";
        labelJmpView->instruction = str + labelJmpView->callProgram ;
    }
    else if(EM_LBLJMP_INSTRUCTION_RUN == labelJmpView->selectInstructionFlag)
    {
        QString str = "RUN ";
        labelJmpView->instruction = str + labelJmpView->callProgram ;
    }

     labelJmpView->instruction =  labelJmpView->instruction + "; " + "//" +  labelJmpView->note;
}
